package com.qf.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/cache")
public class RedisCacheController {

    private StringRedisTemplate redisTemplate;

    @Autowired
    public void setRedisTemplate(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

//    查询单个
    @GetMapping("/get/{key}")
    public String getFromRedis(@PathVariable String key){
//      redis 缓存中查找数据
        String s = redisTemplate.opsForValue().get(key);
        return s;
    }
//  存入缓存
    @PostMapping("/set/{key}")
    public String save2Redis(@PathVariable String key,@RequestBody  String values){

        redisTemplate.opsForValue().set(key, values);
        return "1";
    }


    @PostMapping("/hset/{key}/{field}/{value}")
    public int hSet(@PathVariable String key,@PathVariable String field,@PathVariable String value){
        redisTemplate.opsForHash().put(key,field,value);
        return 1;
    }

    @PostMapping("/hmset/{key}")
    public int hMSet(@PathVariable String key, @RequestBody Map map){
        redisTemplate.opsForHash().putAll(key,map);
        return 2;
    }


}
